<?php
include 'include.php';
	mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die(mysql_error());
	mysql_select_db(DB_NAME);
/*
	mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die(mysql_error());
	mysql_select_db(DB_NAME);
	$sql = "SELECT * from npcs order by npcs_id";
	$result = mysql_query($sql);
	while($myrow = mysql_fetch_row($result)) {
		set_time_limit(30);
		$x = $myrow[2];
		echo "NPC id: " . $myrow[0] . "<br>";
		if(file_exists("c:/xampp/htdocs/wowhead/" . $x . "_zone.txt")) {
			echo "Duplicate zone.<br>";
		}
		else {
*/
$sql = "SELECT * FROM items ORDER BY RAND() LIMIT 10";
$result2 = mysql_query($sql);
while($myrow2 = mysql_fetch_row($result2)) {
	$lookup_item=$myrow2[0];
// for($lookup_item=10001;$lookup_item<50001;$lookup_item++) {
	set_time_limit(60);
	echo "Dump of item " . $lookup_item . " beginning .<br>";
	$result_url = load('http://www.wowarmory.com/item-info.xml?i=' . $lookup_item);
	echo "Dump of item " . $lookup_item . " complete.<br>";
/*
		}
	}
*/
//invalid xml file
	$xmlparser = xml_parser_create();
	$xmldata = $result_url;
	xml_parse_into_struct($xmlparser,$xmldata,$values);
	xml_parser_free($xmlparser);
	$xml_found = 0;
	for($x=0;$x<sizeof($values);$x++) {
		if($values[$x]['tag'] == "ITEM" && $values[$x]['type'] == "open" && $xml_found == 0) {
			$xml_found = $xml_found + 1;
			$items_id = $values[$x]['attributes']['ID'];
			$items_name = mysql_real_escape_string($values[$x]['attributes']['NAME']);
		}
		if($values[$x]['tag'] == "CREATURE") {
			$xml_found = $xml_found + 2;
			$npcs_id = $values[$x]['attributes']['ID'];
			$npcs_name = mysql_real_escape_string($values[$x]['attributes']['NAME']);
			$zone_name = mysql_real_escape_string($values[$x]['attributes']['AREA']);
		}
	}
	echo $xml_found . "<br>";
	if($xml_found > 0) {
		if($xml_found == 1) {
			$npcs_id = "-99";
			$npcs_name = "-99";
			$zone_name = "-99";
		}
		echo "Item ID: " . $items_id . "<br>";
		echo "Item: " . $items_name . "<br>";
		echo "NPC ID: " . $npcs_id . "<br>";
		echo "NPC: " . $npcs_name . "<br>";
		echo "Zone: " . $zone_name . "<br>";
		$sql = "INSERT into items (items_id,items_name,npcs_id,npcs_name,zone_name) VALUES(" . $items_id . ",'" . $items_name . "'," . $npcs_id . ",'" . $npcs_name . "','" . $zone_name . "');";
		echo $sql . "<br>";
		$result = mysql_query($sql);
	}
	else {
		echo "ITEM NOT FOUND <br>";
	}
}
?>
